using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0812367_0912581
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ActionScreen : MyGameScreen
    {
        KeyboardState keyboardState;
        Texture2D image;
        Rectangle imageRectangle;

        //SpriteManager spriteManager;

        GameLevel[] gameLevel;
        GameLevel curGameLevel;

        int nLevel;
        int curLevel;

        public int CurLevel
        {
            get { return curLevel; }
            set { curLevel = value; }
        }

        //public enum 

        public enum ScreenState
        {
            InGame,
            ChangeLevel,
            GameOver, 
            WinGame
        };

        //static ScreenState screenState = ScreenState.InGame;
        public static int screenSate = (int)ScreenState.InGame;

        public ActionScreen(Game game, SpriteBatch spriteBatch, Texture2D image)
            : base(game, spriteBatch)
        {            
            this.image = image;
            imageRectangle = new Rectangle(
                0,
                0,
                Game.Window.ClientBounds.Width,
                Game.Window.ClientBounds.Height);
        }

        public void LoadContent()//****
        {

            nLevel = 4;
            gameLevel = new GameLevel[4];
            curGameLevel = new GameLevel(this.Game, this.spriteBatch);

            gameLevel[0] = new GameLevel1(this.game, this.spriteBatch);
            gameLevel[0].LoadContent(this.Game.Content);
            gameLevel[1] = new GameLevel2(this.game, this.spriteBatch);
            gameLevel[1].LoadContent(this.Game.Content);
            gameLevel[2] = new GameLevel3(this.game, this.spriteBatch);
            gameLevel[2].LoadContent(this.Game.Content);
            gameLevel[3] = new GameLevel4(this.game, this.spriteBatch);
            gameLevel[3].LoadContent(this.Game.Content);

            //curLevel = 0;
            curGameLevel = gameLevel[CurLevel];
            //curLevel++;
            
            
            //spriteManager = new SpriteManager(this.Game, this.spriteBatch);
            //spriteManager.LoadContent(this.Game.Content);
            //this.Components.Add(spriteManager);
            //spriteManager.Enabled = true;
            //spriteManager.Visible = true;
        }

        public override void Update(GameTime gameTime)
        {
            
            keyboardState = Keyboard.GetState();
            //if (keyboardState.IsKeyDown(Keys.Escape))
            //    game.Exit();

            if (GameLevel.monterAcross <= 0)
            {
               
                ActionScreen.screenSate = (int)ScreenState.GameOver;
                GameLevel.monterAcross = 3;
       
        
            }

            if (curGameLevel.stateLevel != (int)GameLevel.StateLevel.End)
            {
                curGameLevel.Update(gameTime);
            }

            else//Ket thuc mot man
            {
                ActionScreen.screenSate = (int)ScreenState.ChangeLevel;
                if (curLevel < (nLevel - 1))
                {
                    curGameLevel = gameLevel[++curLevel];
                    curGameLevel.Update(gameTime);
                }
                else //man cuoi
                {
                    //curGameLevel.Update(gameTime);
                    ActionScreen.screenSate = (int)ScreenState.WinGame;
                }
                    
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);  
            spriteBatch.Draw(image, imageRectangle, Color.White);           
            curGameLevel.Draw(gameTime);

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}